package com.ruoyi.web.controller.seal;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.entity.sealentity.Seal;
import com.ruoyi.common.entity.sealentity.SealPermission;
import com.ruoyi.common.utils.LogUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.web.service.SealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/seal")
@CrossOrigin
public class SealController extends BaseController {

    @Autowired
    private SealService sealService;

    @GetMapping("/hello")
    public Object hello(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return authentication;
    }

    /**
     * 查看印章列表
     * @param seal
     * @return
     */
    @PreAuthorize("@ss.hasPermi('seals:seal:query')")
    @PostMapping("/query")
    public TableDataInfo getSealList(@RequestBody Seal seal){
        startPage();
        List<Seal> list = sealService.getSealList(seal);
        return getDataTable(list);
    }

    /**
     * 新增印章
     * @param seal
     * @return
     */
//    @PreAuthorize("@ss.hasPermi('seals:seal:add')")
    @PostMapping("/add")
    public AjaxResult addSeal(@RequestBody Seal seal){
        int result = sealService.addSeal(seal);
        if (result==1){
            return AjaxResult.success();
        }else
            return AjaxResult.error("新增失败");
    }

    /**
     * 修改印章
     * @param seal
     * @return
     */
//    @PreAuthorize("@ss.hasPermi('seals:seal:edit')")
    @PostMapping("/edit")
    public AjaxResult update(@RequestBody Seal seal){
        int result = sealService.updateSeal(seal);
        if (result==1){
            return AjaxResult.success();
        }else
            return AjaxResult.error("修改失败");
    }

    /**
     * 删除印章
     * @param id
     * @return
     */
//    @PreAuthorize("@ss.hasPermi('seals:seal:delete')")
    @GetMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable("id") String id){
        int result = sealService.deleteSeal(id);
        if (result==1){
            return AjaxResult.success();
        }else
            return AjaxResult.error("修改失败");
    }

    /**
     * 印章授权
     * @param ids 用户id数组
     * @param sealPermission 授权数据
     * @return
     */
//    @PreAuthorize("@ss.hasPermi('seals:authseal:add')")
    @PostMapping("/auth/add/{ids}")
    public AjaxResult addAuthSeal(@PathVariable("ids") Long[] ids, @RequestBody SealPermission sealPermission){
        int result = sealService.AuthSeal(ids, sealPermission);
        if (result==1)
            return AjaxResult.success();
        else
            return AjaxResult.error();
    }

    /**
     * 查看授权列表
     * @param sealPermission
     * @return
     */
//    @PreAuthorize("@ss.hasPermi('seals:authseal:query')")
    @PostMapping("/auth/query")
    public TableDataInfo AuthSealQuery(@RequestBody SealPermission sealPermission){
        startPage();
        List<SealPermission> list = sealService.AuthSealQuery(sealPermission);
        return getDataTable(list);
    }

//    @PreAuthorize("@ss.hasPermi('seals:authseal:edit')")
    @PostMapping("/auth/edit")
    public AjaxResult editAuthSeal(@RequestBody SealPermission sealPermission){
        int result = sealService.editAuthSeal(sealPermission);
        if (result==1){
            return AjaxResult.success();
        }else
            return AjaxResult.error();
    }

    @GetMapping("/auth/delete/{id}")
    public AjaxResult deleteAuthSeal(@PathVariable("id") String id){
        int result = sealService.deleteAuthSeal(id);
        if (result==1){
            return AjaxResult.success();
        }else
            return AjaxResult.error();
    }

    @PostMapping("/uploadfile")
    public AjaxResult uploadFile(@RequestParam("docfile") MultipartFile file) throws IOException {
        if (!file.isEmpty()){
            try {
                String fileName  = FileUploadUtils.upload(RuoYiConfig.getProfile(),file);
                return AjaxResult.success(fileName);
            }catch (Exception e){
                LogUtils.getBlock(e);
                e.printStackTrace();
                return AjaxResult.error("上传失败");
            }
        }else {
            return AjaxResult.error("请选择附件");
        }
    }
}
